// pages/details/details.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: false,
    imgList: [],
    autoplay: false,
    indicatordots: false,
    duration: 500,
    id:'',
    content:'',
    arr:'',
    interestor:false,
    userid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(options) {
    wx.request({
      url: 'http://localhost:3000/login/essay',
      method:'GET',
      data:{
        id:options.id,
        userID:getApp().globalData.user.id,
      },
      success:res=>{
        console.log(res.data);
        this.setData({
          id:options.id,
          arr:res.data,
          userid:res.data[0].user
        })
        wx.request({
          url: 'http://localhost:3000/user/check',
          data:{
            id:getApp().globalData.user.id,
            otherid:this.data.userid,
          },
          success:res=>{
            console.log(res.data)
            if(res.data.count===0){
              this.setData({interestor:false})
            }
            else{
              this.setData({interestor:true})
            }
            console.log(res.data.count)
          },
          fail:err=>{
            console.log(err)
          }
        })
      }
    })
  },
  onSlideChange: function (event) { 
    var postId = event.detail.current; 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  like(e){
    var that = this;
    let id = e.target.id;
    console.log(id);
    wx.request({
      url: 'http://localhost:3000/login/likestate',
      method:'GET',
      data:{
        userId:getApp().globalData.user.id,
        id:id
      }
    })
    that.onLoad(this.data.arr[0]);
  },
  submit(){
      var that = this;
      wx.request({
        url: 'http://localhost:3000/login/comment',
        method:'GET',
        data:{
          userId:getApp().globalData.user.id,
          id:this.data.id,
          content:this.data.content
        }
      })
      that.onLoad(this.data.arr[0]);
  },
  getContent(e){
    this.setData({content:e.detail.value})
  },
  goother(e){
    let id=e.currentTarget.id
    wx.navigateTo({
      url: '../other/other?findid='+id,
    })
  },
  add(){
    let date=new Date();
    date=this.formatDate(date)
    console.log(date)
    wx.request({
      url: 'http://localhost:3000/user/add',
      data:{
        id:getApp().globalData.user.id,
        otherid:this.data.userid,
        fanTime:date
      },
      success:res=>{
        console.log(res)
        this.setData({interestor:true})
        getApp().globalData.user.attention=getApp().globalData.user.attention+1
      },
      fail:err=>{
        console.log(err)
      }
    })
  },
  del(){
    wx.request({
      url: 'http://localhost:3000/user/del',
      data:{
        id:getApp().globalData.user.id,
        otherid:this.data.userid,
      },
      success:res=>{
        this.setData({interestor:false})
        console.log(res)
        getApp().globalData.user.attention=getApp().globalData.user.attention-1
      },
      fail:err=>{
        console.log(err)
      }
    })
  },
  formatDate: function (times) {
    var date = new Date(times);
    var year = date.getFullYear(); //年份
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    var hour = function () { //获取小时
        return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    }
    var minute = function () { //获取分钟
        return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    }

    var second = function () { //获取秒数
        return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    }
    return year + '-' + month + '-' + day + ' ' + hour() + ':' + minute() + ':' + second()

},
  commentLike(e){
    var that = this;
    let id = e.target.id;
    wx.request({
      url: 'http://localhost:3000/login/commentlike',
      method:'GET',
      data:{
        id:id,
      },
    })
    that.onLoad(this.data.arr[0]);
  }
})